package com.istic.android.mmm.model;

import java.util.Date;
import java.util.UUID;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class DataHandler extends DefaultHandler {
	private Zone 		zone;
	private Client      client;
	private Commande    commande;
	private Coordonnees coordonnees;
	
	private UUID zone_uuid, client_uuid, commande_uuid;
	
	private DataManager dm;
	
	@SuppressWarnings("unused")
	private boolean inRoot, inZone, inRue, inCp, inVille, inCoordonnees, inTelephone, inEmail, inClient, inCommande, inNumero, inDate, inEtat;
	private StringBuffer buffer;
	
	public DataHandler(DataManager dataManager) {
		this.dm = dataManager;
	}
	
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
		if(localName.equals("root")){
			Log.w("DataHandler", "Start parsing");
			inRoot = true;
		}
		else if(localName.equals("zone")){
			
			zone	  = new Zone();
			zone_uuid = UUID.randomUUID();
			
			try{
				String  nom = attributes.getValue("nom");
				
				zone.setNom(nom);
			}catch(Exception e){
				throw new SAXException(e);
			}
			inZone = true;
		}
		else if(localName.equals("client")){
			client		= new Client();
			client_uuid = UUID.randomUUID();
			client.setZone(zone);
			
			try{
				String nom  = attributes.getValue("nom");
				int    etat = Integer.parseInt(attributes.getValue("etat"));
				
				client.setNom(nom);
				client.setEtat(etat);
			}catch(Exception e){
				throw new SAXException(e);
			}
			inClient = true;
		}
		else {
			buffer = new StringBuffer();
			if(localName.equals("coordonnees")){
				coordonnees = new Coordonnees();
				inCoordonnees = true;
			}
			else if(localName.equals("commande")){
				commande = new Commande();
				commande_uuid = UUID.randomUUID();
				commande.setClient(client);
				inCommande = true;
			}
			else if(localName.equals("rue")){
				inRue = true;
			}
			else if(localName.equals("cp")){
				inCp = true;
			}
			else if(localName.equals("ville")){
				inVille = true;
			}
			else if(localName.equals("numero")){
				inNumero = true;
			}
			else if(localName.equals("date")){
				inDate = true;
			}
			else if(localName.equals("etat")){
				inEtat = true;
			}
			else if(localName.equals("telephone")){
				inTelephone = true;
			}
			else if(localName.equals("email")){
				inEmail = true;
			}
			else{
				throw new SAXException("Balise "+localName+" inconnue.");
			}
		}
	}
	public void endElement(String uri, String localName, String qName) throws SAXException{
		if(localName.equals("root")){
			inRoot = false;
		}
		else if(localName.equals("zone")){
			dm.addZone(zone_uuid, zone);
			zone = null;
			inZone = false;
		}
		else if(localName.equals("client")){
			dm.addClient(client_uuid, client);
			zone.addClient(client_uuid);
			client = null;
			inClient = false;
		}
		else if(localName.equals("commande")){
			dm.addCommande(commande_uuid, commande);
			client.addCommande(commande_uuid);
			commande = null;
			inCommande = false;
		}
		else if(localName.equals("coordonnees")){
			client.setCoordonnees(coordonnees);
			coordonnees = null;
			inCoordonnees = false;
		}
		else if(localName.equals("ville")){
			coordonnees.setVille(buffer.toString());
			buffer = null;
			inVille = false;
		}
		else if(localName.equals("rue")){
			coordonnees.setRue(buffer.toString());
			buffer = null;
			inRue = false;
		}
		else if(localName.equals("cp")){
			coordonnees.setCp(buffer.toString());
			buffer = null;
			inCp = false;
		}
		else if(localName.equals("email")){
			coordonnees.setEmail(buffer.toString());
			buffer = null;
			inEmail = false;
		}
		else if(localName.equals("telephone")){
			coordonnees.setTelephone(buffer.toString());
			buffer = null;
			inTelephone = false;
		}
		else if(localName.equals("numero")){
			commande.setNumero(buffer.toString());
			buffer = null;
			inNumero = false;
		}
		else if(localName.equals("date")){
			//A REVOIR
			commande.setDate(new Date(buffer.toString()));
			buffer = null;
			inDate = false;
		}
		else if(localName.equals("etat")){
			commande.setEtat(Integer.parseInt(buffer.toString()));
			buffer = null;
			inDate = false;
		}
		else{
			throw new SAXException("Balise "+localName+" inconnue.");
		}          
	}
	public void characters(char[] ch,int start, int length) throws SAXException{
		String lecture = new String(ch,start,length);
		if(buffer != null) buffer.append(lecture);       
	}
}